package cn.yiufung.qxquartz.controller;

import cn.yiufung.qxquartz.common.ApiResponse;
import cn.yiufung.qxquartz.entity.SystemScheduledDO;
import cn.yiufung.qxquartz.service.SystemScheduledService;
import cn.yiufung.qxquartz.utils.CronUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 定时任务控制层
 * @author 姚锋
 * @createTime 2023年08月18日 11:00:00
 */
@CrossOrigin
@RestController
public class ScheduledController {

    private static final Logger log = LoggerFactory.getLogger(ScheduledController.class);

    @Autowired
    private SystemScheduledService systemScheduledService;

    @GetMapping("/scheduled/getList")
    public ApiResponse<PageInfo<SystemScheduledDO>> getList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                       @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        try {
            PageHelper.startPage(pageNum, pageSize);
            List<SystemScheduledDO> list = systemScheduledService.list(new QueryWrapper<SystemScheduledDO>()
                    .orderByDesc("create_time"));
            PageInfo<SystemScheduledDO> pageInfo = new PageInfo<>(list);
            return ApiResponse.success(pageInfo);
        }catch (Exception e){
            log.error("获取定时任务列表异常", e);
            return ApiResponse.internalServerError("获取定时任务列表异常：" + e);
        }
    }

    @PostMapping(("/scheduled/pause/{id}"))
    public ApiResponse<Void> pause(@PathVariable Long id){
        try {
            systemScheduledService.pauseJob(id);
        }catch (Exception e){
            log.error("暂停定时任务异常", e);
            return ApiResponse.internalServerError("暂停定时任务异常：" + e.getMessage());
        }
        return ApiResponse.success();
    }

    @PostMapping(("/scheduled/resume/{id}"))
    public ApiResponse<Void> resume(@PathVariable Long id){
        try {
            systemScheduledService.resumeJob(id);
        }catch (Exception e){
            log.error("继续定时任务异常", e);
            return ApiResponse.internalServerError("继续定时任务异常：" + e);
        }
        return ApiResponse.success();
    }

    @PostMapping(("/scheduled/delete/{id}"))
    public ApiResponse<Void> delete(@PathVariable Long id){
        try {
            systemScheduledService.deleteJob(id);
        }catch (Exception e){
            log.error("删除定时任务异常", e);
            return ApiResponse.internalServerError("删除定时任务异常：" + e);
        }
        return ApiResponse.success();
    }

    @PostMapping(("/scheduled/run/{id}"))
    public ApiResponse<Void> run(@PathVariable Long id){
        try {
            systemScheduledService.run(id);
        }catch (Exception e){
            log.error("执行一次任务异常", e);
            return ApiResponse.internalServerError("执行一次任务异常：" + e);
        }
        return ApiResponse.success();
    }

    @PostMapping(("/scheduled/addScheduled"))
    public ApiResponse<Void> addScheduled(@RequestBody SystemScheduledDO systemScheduledDO){
        try {
            //不做严格校验
            if(!CronUtils.isValid(systemScheduledDO.getCronExpression())){
                return ApiResponse.internalServerError("cron表达式不对");
            }
            systemScheduledService.addScheduled(systemScheduledDO);
        }catch (Exception e){
            log.error("添加任务异常", e);
            return ApiResponse.internalServerError("添加任务异常：" + e);
        }
        return ApiResponse.success();
    }

    @PostMapping(("/scheduled/updateScheduled"))
    public ApiResponse<Void> updateScheduled(@RequestBody SystemScheduledDO systemScheduledDO){
        try {
            //不做严格校验
            if(!CronUtils.isValid(systemScheduledDO.getCronExpression())){
                return ApiResponse.internalServerError("cron表达式不对");
            }
            systemScheduledService.updateScheduled(systemScheduledDO);
        }catch (Exception e){
            log.error("修改任务异常", e);
            return ApiResponse.internalServerError("修改任务异常：" + e);
        }
        return ApiResponse.success();
    }

}
